<?php

namespace IYUU\sdk\CoolQ\Modules\basic;

use BasicReplyModel;
use IYUU\sdk\CoolQ\Context;
use IYUU\sdk\CoolQ\Event\Message as EventMessage;
use IYUU\sdk\CoolQ\ModulesProcessor;

/**
 * 文本消息匹配
 * - 规则：按优先级依次匹配，匹配模式（关键字包含、内容等价、正则表达式）
 * Class processor
 * @package IYUU\sdk\CoolQ\Modules\basic
 */
class processor extends ModulesProcessor
{
    /**
     * 应答方法 （抽象方法，子类实现）
     * @param Context $context
     * @param EventMessage $msgObj
     * @return array|null
     */
    public function respond(Context $context, EventMessage $msgObj): ?array
    {
        // 1.取消息、取Session
        $session_key = Context::moduleRuleKeywordSession;
        $ruleKeyword = $context->getSession($session_key);
        $rid = $ruleKeyword['rid'];

        // 2.查表
        $reply = BasicReplyModel::where('rid', $rid)->findOrEmpty();
        if ($reply->isEmpty()) {
            return null;
        }
        $item = $reply->toArray();
        $msg = PHP_EOL . 'rid:' . $rid;
        return $msgObj->isPrivate() ? EventMessage::reply($item['content']) : EventMessage::replyGroup($item['content'] . $msg, false, true);
    }
}
